/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.main.pojo.VO;

import com.tbit.main.constant.MachineLocationTypeConstant;
import com.tbit.main.constant.WorkOrderConstant;
import com.tbit.main.pojo.AccountUserWorkConfig;
import com.tbit.main.pojo.WorkOrderFinishing;
import lombok.Data;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 我的待办工单数量
 *
 * @author <a href="mailto:li.yao@tbit.com.cn">yao</a>
 * @version 3.8.9.1
 * @since 2024/3/13 13:32
 */
@Data
public class WorkOrderFinishingCountRespVO {

    /**
     * 工单总数
     */
    private Integer workOrderTotal = 0;

    /**
     * 换电工单数量
     */
    private Integer powerChangeWorkOrderCount = 0;

    /**
     * 站外挪车工单数量
     */
    private Integer outSiteMoveWorkOrderCount = 0;

    /**
     * 超区挪车工单数量
     */
    private Integer superZoneMoveWorkOrderCount = 0;

    /**
     * 调度工单数量
     */
    private Integer dispatchWorkOrderCount = 0;

    /**
     * 维修工单数量
     */
    private Integer repairWorkOrderCount = 0;

    /**
     * 手工工单数量
     */
    private Integer labourWorkOrderCount = 0;

    /**
     * 运维人员编号
     */
    private Integer accountUserId;

    /**
     * 运维人员名称
     */
    private String accountUserName;


    public void add(WorkOrderFinishingCountRespVO workOrderFinishingCountAll, WorkOrderFinishing workOrderFinishing) {
        Integer workOrderType = workOrderFinishing.getWorkType();
        Integer locationType = workOrderFinishing.getLocationType();
        Integer buildType = workOrderFinishing.getBuildType();

        this.workOrderTotal++;
        workOrderFinishingCountAll.workOrderTotal++;

        if (Objects.equals(buildType, 1)) {
            if (Objects.equals(workOrderType, WorkOrderConstant.workType_HD)) { // 换电
                this.powerChangeWorkOrderCount++;
                workOrderFinishingCountAll.powerChangeWorkOrderCount++;
            }

            if (Objects.equals(workOrderType, WorkOrderConstant.workType_NC) && Objects.equals(locationType, MachineLocationTypeConstant.OUT_SITE_MOVE)) { // 站外
                this.outSiteMoveWorkOrderCount++;
                workOrderFinishingCountAll.outSiteMoveWorkOrderCount++;
            }

            if (Objects.equals(workOrderType, WorkOrderConstant.workType_NC) && Objects.equals(locationType, MachineLocationTypeConstant.SUPER_ZONE_MOVE)) { // 超区
                this.superZoneMoveWorkOrderCount++;
                workOrderFinishingCountAll.superZoneMoveWorkOrderCount++;
            }

            if (Objects.equals(workOrderType, WorkOrderConstant.workType_WX)) { // 维修
                this.repairWorkOrderCount++;
                workOrderFinishingCountAll.repairWorkOrderCount++;
            }

            if (Objects.equals(workOrderType, WorkOrderConstant.workType_DD)) { // 调度
                this.dispatchWorkOrderCount++;
                workOrderFinishingCountAll.dispatchWorkOrderCount++;
            }
        }

        if (Objects.equals(buildType, 2)) { // 手工
            this.labourWorkOrderCount++;
            workOrderFinishingCountAll.labourWorkOrderCount++;
        }

        this.accountUserId = workOrderFinishing.getGetUserId();
        this.accountUserName = workOrderFinishing.getGetUserName();
    }


    public void add(WorkOrderFinishingCountRespVO workOrderFinishingCountAll, WorkOrderFinishing workOrderFinishing, List<AccountUserWorkConfig> accountUserWorkTypeList) {
        Integer buildType = workOrderFinishing.getBuildType();

        if (Objects.equals(buildType, WorkOrderConstant.buildType_H)) { // 手工
            this.labourWorkOrderCount++;
            workOrderFinishingCountAll.labourWorkOrderCount++;
        }

        this.accountUserId = workOrderFinishing.getGetUserId();
        this.accountUserName = workOrderFinishing.getGetUserName();
        this.workOrderTotal++;
        workOrderFinishingCountAll.workOrderTotal++;

        if (Objects.equals(buildType, WorkOrderConstant.buildType_V)) { // 自动
            // 工单临时类型
            Integer workOrderTypeTemp = getWorkOrderType(workOrderFinishing, accountUserWorkTypeList);
            if (null == workOrderTypeTemp) { // 不包含此工种
                return;
            }

            if (workOrderTypeTemp.equals(WorkOrderTypeTemp.workType_HD)) { // 换电
                powerChangeWorkOrderCount = Optional.ofNullable(powerChangeWorkOrderCount).orElse(0) + 1;
                workOrderFinishingCountAll.powerChangeWorkOrderCount = Optional.ofNullable(workOrderFinishingCountAll.powerChangeWorkOrderCount).orElse(0) + 1;
            }

            if (workOrderTypeTemp.equals(WorkOrderTypeTemp.workType_superZone_NC)) { // 超区
                superZoneMoveWorkOrderCount = Optional.ofNullable(superZoneMoveWorkOrderCount).orElse(0) + 1;
                workOrderFinishingCountAll.superZoneMoveWorkOrderCount = Optional.ofNullable(workOrderFinishingCountAll.superZoneMoveWorkOrderCount).orElse(0) + 1;
            }

            if (workOrderTypeTemp.equals(WorkOrderTypeTemp.workType_outSite_NC)) { // 站外
                outSiteMoveWorkOrderCount = Optional.ofNullable(outSiteMoveWorkOrderCount).orElse(0) + 1;
                workOrderFinishingCountAll.outSiteMoveWorkOrderCount = Optional.ofNullable(workOrderFinishingCountAll.outSiteMoveWorkOrderCount).orElse(0) + 1;
            }

            if (workOrderTypeTemp.equals(WorkOrderTypeTemp.workType_WX)) { // 维修
                repairWorkOrderCount = Optional.ofNullable(repairWorkOrderCount).orElse(0) + 1;
                workOrderFinishingCountAll.repairWorkOrderCount = Optional.ofNullable(workOrderFinishingCountAll.repairWorkOrderCount).orElse(0) + 1;
            }

            if (workOrderTypeTemp.equals(WorkOrderTypeTemp.workType_DD)) { // 调度
                dispatchWorkOrderCount = Optional.ofNullable(dispatchWorkOrderCount).orElse(0) + 1;
                workOrderFinishingCountAll.dispatchWorkOrderCount = Optional.ofNullable(workOrderFinishingCountAll.dispatchWorkOrderCount).orElse(0) + 1;
            }
        }

    }


    /**
     * 转换为临时类型
     * @param workOrderFinishing 待办工单
     * @param accountUserWorkTypeList 所属工种
     * @return 临时工单类型
     */
    public Integer getWorkOrderType(WorkOrderFinishing workOrderFinishing, List<AccountUserWorkConfig> accountUserWorkTypeList) {
        Integer workOrderType = null;

        Integer workType = workOrderFinishing.getWorkType();
        Integer locationType = workOrderFinishing.getLocationType();

        for (AccountUserWorkConfig accountUserWorkConfig : accountUserWorkTypeList) {
            if (workType.equals(accountUserWorkConfig.getWorkType())) { // 包含此工种

                if (workType.equals(WorkOrderConstant.workType_NC)
                        && locationType.equals(accountUserWorkConfig.getLocationType())) {
                    if (locationType.equals(MachineLocationTypeConstant.SUPER_ZONE_MOVE)) { // 包含超区
                        workOrderType = WorkOrderTypeTemp.workType_superZone_NC;
                    }
                    if (locationType.equals(MachineLocationTypeConstant.OUT_SITE_MOVE)) { // 包含站外
                        workOrderType = WorkOrderTypeTemp.workType_outSite_NC;
                    }
                }

                if (workType.equals(WorkOrderConstant.workType_HD)) {
                    workOrderType = WorkOrderTypeTemp.workType_HD;
                }

                if (workType.equals(WorkOrderConstant.workType_WX)) {
                    workOrderType = WorkOrderTypeTemp.workType_WX;
                }

                if (workType.equals(WorkOrderConstant.workType_DD)) {
                    workOrderType = WorkOrderTypeTemp.workType_DD;
                }
            }
        }
        return workOrderType;
    }

    /**
     * 工单临时类型
     */
    public static class WorkOrderTypeTemp {
        //换电
        public final static Integer workType_HD = 1;
        //超区挪车
        public final static Integer workType_superZone_NC = 2;
        //站外挪车
        public final static Integer workType_outSite_NC = 3;
        //维修
        public final static Integer workType_WX = 4;
        //调度
        public final static Integer workType_DD = 5;
    }



}
